package com.xqq.utils;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.text.StrBuilder;
import com.xqq.dao.TbTrackDAO;
import com.xqq.entity.TbTrackDO;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;

@Component
public class TrackDataGenerator implements ApplicationRunner {


    public static List<TbTrackDO> generateTrackData(String routeString, String startTime, long timeInterval, long uid, int distance) {
        List<TbTrackDO> trackList = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        int count = 0;
        try {
            Date baseDate = dateFormat.parse(startTime);
            String[] points = routeString.split(";");
            double[] previousPoint = null;
            for (String point : points) {
                if (point.trim().isEmpty()) continue; // 跳过空字符串
                String[] coords = point.split(",");
                double currentLon = Double.parseDouble(coords[0]);
                double currentLat = Double.parseDouble(coords[1]);

                if (previousPoint != null) {
                    List<double[]> newPoints = interpolatePoints(previousPoint, new double[]{currentLat, currentLon}, distance);
                    for (int i = 0; i < newPoints.size(); i++) {
                        double[] interpolatedPoint = newPoints.get(i);
                        Date currentTime = new Date(baseDate.getTime() + trackList.size() * timeInterval);
                        TbTrackDO track = new TbTrackDO();
                        track.setUid(uid); // 示例人员ID
//                        System.out.println(count+"=>"+interpolatedPoint[1]+","+interpolatedPoint[0]);
                        track.setPosition(String.format("%6f,%6f", interpolatedPoint[1], interpolatedPoint[0])); // 设置经纬度
                        track.setKeyPoint(1); // 默认不是重要点
                        track.setGmtCreate(currentTime);
                        track.setGmtUpdate(DateTime.now());
                        track.setDate(simpleDateFormat.format(currentTime));
                        trackList.add(track);
                        count++;
                    }
                }
                previousPoint = new double[]{currentLat, currentLon};
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return trackList;
    }

    private static List<double[]> interpolatePoints(double[] start, double[] end, double distance) {
        List<double[]> points = new ArrayList<>();
        double earthRadius = 6371000; // 地球半径（米）
        double dLat = Math.toRadians(end[0] - start[0]);
        double dLon = Math.toRadians(end[1] - start[1]);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(Math.toRadians(start[0])) * Math.cos(Math.toRadians(end[0])) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double totalDistance = earthRadius * c; // 总距离（米）
        int numPoints = (int) Math.floor(totalDistance / distance);
        if (numPoints != 0) {
            for (int i = 0; i <= numPoints; i++) {
                double fraction = (double) i / numPoints;
                double lat = start[0] + fraction * (end[0] - start[0]);
                double lon = start[1] + fraction * (end[1] - start[1]);
                points.add(new double[]{lat, lon});
            }
        }
        return points;
    }

    @Resource
    private TbTrackDAO trackDAO;

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        String routeString = RouteConstans.ROUTE_XIXIAN_A; // 示例路线字符串
//        long timeInterval = 4000; // 每个点增加的时间（毫秒）
//        int distance = 50;
//
//        String startTime = "2024-12-05 04:00:00"; // 起始时间
//        long uid = 360663254847344640L;//前导车
//        List<TbTrackDO> trackData = generateTrackData(routeString, startTime, timeInterval, uid, distance);
//        trackDAO.saveBatch(trackData);
//        System.out.println("Total points: " + trackData.size());
//
//        startTime = "2024-12-05 04:05:00";
//        uid = 360663254843150336L;// 前哨车
//        trackData = generateTrackData(routeString, startTime, timeInterval, uid, distance);
//        trackDAO.saveBatch(trackData);
//        System.out.println("Total points: " + trackData.size());
//
//        startTime = "2024-12-05 04:06:00";
//        uid = 358463104243949569L;// 杨东卫
//        trackData = generateTrackData(routeString, startTime, timeInterval, uid, distance);
//        trackDAO.saveBatch(trackData);
//        System.out.println("Total points: " + trackData.size());
//
//        startTime = "2024-12-05 04:07:00";
//        uid = 360663254855733248L;// 应急车
//        trackData = generateTrackData(routeString, startTime, timeInterval, uid, distance);
//        trackDAO.saveBatch(trackData);
//        System.out.println("Total points: " + trackData.size());
//
//        startTime = "2024-12-05 04:09:00";
//        uid = 360663254838956032L;// 后卫车
//        trackData = generateTrackData(routeString, startTime, timeInterval, uid, distance);
//        trackDAO.saveBatch(trackData);
//        System.out.println("Total points: " + trackData.size());
//
//        startTime = "2024-12-05 04:10:00";
//        uid = 360663254813790208L;// 夏东仑
//        trackData = generateTrackData(routeString, startTime, timeInterval, uid, distance);
//        trackDAO.saveBatch(trackData);
//        System.out.println("Total points: " + trackData.size());
//
//        startTime = "2024-12-05 04:11:00";
//        uid = 360663254817984512L;// 黄磊
//        trackData = generateTrackData(routeString, startTime, timeInterval, uid, distance);
//        trackDAO.saveBatch(trackData);
//        System.out.println("Total points: " + trackData.size());
//
//        startTime = "2024-12-05 04:12:00";
//        uid = 358463104252338176L;// 岳扬
//        trackData = generateTrackData(routeString, startTime, timeInterval, uid, distance);
//        trackDAO.saveBatch(trackData);
//        System.out.println("Total points: " + trackData.size());
//
//        startTime = "2024-12-05 00:13:00";
//        uid = 358463104122314752L;// V1-1
//        trackData = generateTrackData(routeString, startTime, timeInterval, uid, distance);
//        trackDAO.saveBatch(trackData);
//        System.out.println("Total points: " + trackData.size());
    }
}
